package com.c.ctgproject.adapter.guang

import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityMyPartInputBinding
import com.c.ctgproject.entity.guang.PartInWarehouseEntity
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.*

/**
 *作者:LiBW
 *创建日期: 2021/10/9
 *描述: 我的散件录入 页面 (配件)
 */
class MyPartInputActivity : BaseActivityV2() , PartInWarehouseAdapter.PartInWarehouseCallBack {
    private val mBinding by lazy { ActivityMyPartInputBinding.inflate(layoutInflater) }
    override fun getLayout() = mBinding.root
    private var page = 1
    private var search: String = ""
    private val mList = ArrayList<PartInWarehouseEntity.DataBean.ListBean>()
    private val mAdapter by lazy { PartInWarehouseAdapter(this, mList, true, 1,null,this) }

    override fun event() {
        getData()
    }

    override fun click() {
        mBinding.mCarRV.adapter = mAdapter
        mBinding.mCarRV.layoutManager = LinearLayoutManager(this)


        mBinding.mSearchET.handlerText {
            if (it.isEmpty() || it == "") {
                search = ""
                page = 1
                getData()
            }
        }
        mBinding.mSearchTV.setOnClickListener {
            search = mBinding.mSearchET.text.handlerNull()
            page = 1
            getData()
        }
        mBinding.mRefresh.setOnLoadMoreListener {
            page++
            getData()
        }

        mBinding.mRefresh.setOnRefreshListener {
            page = 1
            getData()
        }
    }

    fun getData() {
        ApiMethods.apiSubscribe(ApiMethods.retrofit.myPartInputPoundV2(1, search, page), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
            MyLog.d("我的散件录入:$it")
            if (page == 1) mList.clear()
            val data = getGson().fromJson(it, PartInWarehouseEntity::class.java).data
            data?.list?.let { list ->
                mList.addAll(list)
                mAdapter.notifyDataSetChanged()
                mBinding.mCarRV.handlerPosition(page)
            }
        })
    }

    override fun itemDelete(position: Int, data: PartInWarehouseEntity.DataBean.ListBean) {
        
    }
}